﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.Globalization;
using System.Security.Permissions;
using System.Security.Principal;

#endregion

namespace ZO.SmartCore.My4CSharp
{
    /// <summary>Provides a type converter to convert <see cref="BuiltInRole"></see> 
    /// enumeration values to <see cref="T:System.Security.Principal.WindowsBuiltInRole"></see> enumeration values.</summary>
    [HostProtection(SecurityAction.LinkDemand, SharedState = true)]
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    public class BuiltInRoleConverter : TypeConverter
    {

        #region Constructors

        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        #endregion

        #region Methods
        /// <summary>Gets a value indicating whether this converter can convert an object to the given destination type using the context.</summary>
        /// <returns>A <see cref="T:System.Boolean"></see> object that indicates whether this converter can perform the conversion.</returns>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> object that provides a format context. </param>
        /// <param name="destinationType">A <see cref="T:System.Type"></see> object that represents the type you wish to convert to. </param>
        /// <filterpriority>1</filterpriority>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if ((destinationType != null) && destinationType.Equals(typeof(WindowsBuiltInRole)))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }


        /// <summary>Converts the given object to another type.</summary>
        /// <returns>The converted object.</returns>
        /// <param name="culture">The culture into which value will be converted.</param>
        /// <param name="context">A formatter context. </param>
        /// <param name="destinationType">The type to convert the object to. </param>
        /// <param name="value">The object to convert. </param>
        /// <exception cref="T:System.ArgumentNullException">destinationType is Nothing.</exception>
        /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
        /// <filterpriority>1</filterpriority>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if ((destinationType != null) && destinationType.Equals(typeof(WindowsBuiltInRole)))
            {
                User.ValidateBuiltInRoleEnumValue((BuiltInRole)value, "value");
                return this.GetWindowsBuiltInRole(value);
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }


        private WindowsBuiltInRole GetWindowsBuiltInRole(object role)
        {
            string text1 = Enum.GetName(typeof(BuiltInRole), role);
            object obj1 = Enum.Parse(typeof(WindowsBuiltInRole), text1);
            if (obj1 != null)
            {
                return (WindowsBuiltInRole)obj1;
            }
            return (WindowsBuiltInRole)role;
        }

 

        #endregion
    }
}
